package com.java2007.hotel.dao.impl;

import com.java2007.hotel.dao.OrderDao;
import com.java2007.hotel.pojo.Order;
import com.java2007.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/28
 **/
public class OrderDaoImpl implements OrderDao {
    /**
     * 添加订单
     * @param order
     * @return
     */
    @Override
    public int addOrder(Order order) throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "INSERT INTO t_order " +
                "(order_id,order_table_id,order_price,order_pay_status,order_create_time,order_update_time,order_cteate_user_id)" +
                "VALUES(?,?,?,?,?,?,?)";
        return queryRunner.update(JdbcUtils.getConnection(),sql,order.getOrderId(),order.getOrderTableId(),order.getOrderPrice(),order.getOrderPayStatus(),order.getOrderCreateTime(),order.getOrderUpdateTime(),order.getOrderCteateUserId());
    }

    /**
     * 修改订单
     * @return
     */
    @Override
    public int updateOrder(Order order) throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "UPDATE t_order " +
                "SET order_table_id = ?,order_price = ?,order_pay_status = ?,order_update_time = ?,order_cteate_user_id = ? " +
                "WHERE order_id = ?";
        return queryRunner.update(JdbcUtils.getConnection(),sql,order.getOrderTableId(),order.getOrderPrice(),order.getOrderPayStatus(),order.getOrderUpdateTime(),order.getOrderCteateUserId(),order.getOrderId());
    }

    /**
     * 查询所有订单
     * @return
     * @throws SQLException
     */
    @Override
    public List<Order> findAllOrder() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT " +
                "order_id orderId,o.order_table_id orderTableId,t.table_name orderTableName,order_price orderPrice,order_pay_status orderPayStatus,order_create_time orderCreateTime,order_update_time orderUpdateTime,order_cteate_user_id orderCteateUserId " +
                "FROM t_order o LEFT JOIN t_dinner_table t " +
                "ON o.order_table_id = t.table_id";
        return queryRunner.query(sql,new BeanListHandler<>(Order.class));
    }

    /**
     * 通过订单id查询订单
     * @param orderId
     * @return
     * @throws SQLException
     */
    @Override
    public Order findOrder(Long orderId) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT " +
                "order_id orderId,o.order_table_id orderTableId,t.table_name orderTableName,order_price orderPrice,order_pay_status orderPayStatus,order_create_time orderCreateTime,order_update_time orderUpdateTime,order_cteate_user_id orderCteateUserId " +
                "FROM t_order o LEFT JOIN t_dinner_table t " +
                "ON o.order_table_id = t.table_id " +
                "WHERE o.order_id = ?";
        return queryRunner.query(sql,new BeanHandler<>(Order.class),orderId);
    }

    /**
     * 通过用户id查询用户下的订单
     * @param userId
     * @return
     */
    @Override
    public List<Order> findOrderByCreatUserId(Integer userId) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT " +
                "order_id orderId,o.order_table_id orderTableId,t.table_name orderTableName,order_price orderPrice,order_pay_status orderPayStatus,order_create_time orderCreateTime,order_update_time orderUpdateTime,order_cteate_user_id orderCteateUserId " +
                "FROM " +
                "t_order o LEFT JOIN t_dinner_table t " +
                "ON o.order_table_id = t.table_id " +
                "WHERE o.order_cteate_user_id = ? " +
                "ORDER BY o.order_create_time DESC";
        return queryRunner.query(sql,new BeanListHandler<>(Order.class),userId);
    }


}
